﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Mail;

namespace PackToKindle
{
    class Mailer : IMailer
    {
        public Mailer(MailSettings credentilas)
        {
            _credentials = credentilas;
        }

        public void SendMail(bool convert, IEnumerable<string> zipFilePaths)
        {
            Debug.Assert(zipFilePaths != null, "zipFilePath is null.");

            string from = _credentials.MailAddress;
            string to = _credentials.SendToKindleAddress;

            MailMessage message = new MailMessage(from, to, convert ? "convert" : string.Empty, string.Empty);

            foreach (var file in zipFilePaths)
                message.Attachments.Add(new Attachment(file));

            SmtpClient client = new SmtpClient(_credentials.SmtpServer);
            client.Port = _credentials.SmtpPort;
            client.Credentials = _credentials;

            client.Send(message);

            // Need to do dispose after attaching.
            message.Attachments.Dispose();
        }

        private MailSettings _credentials;
    }
}
